using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ClickNET;
using ClickNET.Imaging;
using ClickNET.Web.UI;

public partial class usercontrols_PhotoGallery_SingleImageView : ClickNET.Web.UI.UserControl
{
    private string m_cameraMake, m_dateTaken, m_exposure, m_fstop, m_iso;
    private int album_img_count;
    private int album_img_current;
    int currentImgIndex;
    private ArrayList albumImgArr;
    private ExifProcessor m_exifProcessor;

    private const string EMPTY_LINK = "javascript:return false;";
    private const string IMAGE_PAGE_URL_FORMAT = "/viewimage.aspx?id={0}";
    private const int THUMB_SIZE = 400;

    public int ImageID
    {
        get;
        set;
    }

    public Picture Picture
    {
        get;
        set;
    }

    public List<ExifDetailItem> ExifDetails
    {
        get;
        set;
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        base.Page.AddCssFile("viewimage.css");

        if (null != this.Picture && this.Picture.Exists())
        {
            this.LoadExifData();
            this.BindControls();
        }
        else
        {
            this.c_viewImagePanel.Visible = false;
            this.c_noImagePanel.Visible = true;
        }
    }

    private void BindControls()
    {
        this.c_imageLink.NavigateUrl = this.Picture.Url;
        this.c_fullSizeLink.NavigateUrl = this.Picture.Url;
        this.c_image.ImageUrl = FileSystemGalleryManager.GetThumbnailUrl(this.Picture, THUMB_SIZE);

        this.c_exifDetailsRptr.DataSource = this.ExifDetails;
        this.c_exifDetailsRptr.DataBind();

        /*
        DBCmd dbc = new DBCmd();
        albumImgArr = dbc.GetAlbumImageCount(this.Picture.FolderName);
        currentImgIndex = albumImgArr.IndexOf(this.Picture.ID.ToString());

        this.c_countLabel.Text = string.Format("Image {0} of {1}", currentImgIndex + 1, albumImgArr.Count);

        if (currentImgIndex + 1 < albumImgArr.Count)
        {
            int nextImgId = currentImgIndex + 1;
            c_nextImageLink.NavigateUrl = string.Format(IMAGE_PAGE_URL_FORMAT, albumImgArr[nextImgId].ToString());
        }

        if (currentImgIndex - 1 != -1)
        {
            int prevImgId = currentImgIndex - 1;
            c_previousImageLink.NavigateUrl = string.Format(IMAGE_PAGE_URL_FORMAT, albumImgArr[prevImgId].ToString());
        }
        */
    }

    private void LoadExifData()
    {
        if (null != this.Picture && this.Picture.Exists())
        {
            this.m_exifProcessor = new ExifProcessor(this.Picture.Path, null, null);
            this.ExifDetails = new List<ExifDetailItem>();

            this.TryGetExifValue("Equip Model", out m_cameraMake);
            this.TryGetExifValue("DTOrig", out m_dateTaken);
            this.TryGetExifValue("Shutter Speed", out m_exposure);
            this.TryGetExifValue("Aperture", out m_fstop);
            this.TryGetExifValue("ISO Speed", out m_iso);

            if (!String.IsNullOrEmpty(this.m_cameraMake))
                this.AddExifDetailItem("Camera", m_cameraMake);

            if (!String.IsNullOrEmpty(this.m_dateTaken))
                this.AddExifDetailItem("Date Taken", this.ParseExifDateString(m_dateTaken).ToString("dddd, dd MMMM yyyy"));

            if (!String.IsNullOrEmpty(this.m_exposure))
                this.AddExifDetailItem("Exposure", m_exposure);

            if (!String.IsNullOrEmpty(this.m_fstop))
                this.AddExifDetailItem("F-Stop", m_fstop);

            if (!String.IsNullOrEmpty(this.m_iso))
                this.AddExifDetailItem("ISO Speed", m_iso);
        }
    }

    private void TryGetExifValue(string keyName, out string value)
    {
        char[] badChars = { '\0' };

        value = string.Empty;
        if (null != this.m_exifProcessor && null != this.m_exifProcessor[keyName])
        {
            value = this.m_exifProcessor[keyName].ToString();
            value = value.Trim(badChars);
        }
    }

    public DateTime ParseExifDateString(string exifDateString)
    {
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;

        if (!String.IsNullOrEmpty(exifDateString))
        {
            string[] timestampTokens = exifDateString.Split(' ');

            // Parse out date
            string dateString = timestampTokens[0];
            string[] dateTokens = dateString.Split(':');
            int.TryParse(dateTokens[0], out year);
            int.TryParse(dateTokens[1], out month);
            int.TryParse(dateTokens[2], out day);

            // Parse out time
            string timeString = timestampTokens[1];
            string[] timeTokens = timeString.Split(':');
            int.TryParse(timeTokens[0], out hour);
            int.TryParse(timeTokens[1], out minute);
            int.TryParse(timeTokens[2], out second);

            if (year == 0)
                return DateTime.MinValue;

            return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified);
        }

        return DateTime.MinValue;
    }

    private void AddExifDetailItem(string description, string value)
    {
        this.ExifDetails.Add(
            new ExifDetailItem { Description = description, Value = value.TrimEnd('\0') }
            );
    }
}

public class ExifDetailItem
{
    public string Description;
    public string Value;
}
